#include <C8051F330.h>
#include "misc.h"
#include "BLMC.h"
#include "define.h"
#include "AppGlobalData.h"

Bool TimerDelayus(UINT16 Delayus)
{
	TMR2H = (65535 - Delayus*2)/256;
	TMR2L = (65535 - Delayus*2)%256;
	TM2_INT_FLAG = INT_NOT_COME;
	TMR2CN |= 0x04;	
	while(TM2_INT_FLAG == INT_NOT_COME);
	TMR2CN &= 0xFB;; 
	return 0;
}
Bool TimerDelayms(UINT16 Delayms)
{
	UINT16 i;

	for(i = 0; i<Delayms; i++)
	{
		while(TimerDelayus(1000));
	}
	return 0;
}

void Beep(UINT8 note,UINT16 tonelength, UINT16 pauselength)
{
	UINT16 i;
	FET_OFF;

	SetPWM(77);//15%pwm
	for(i = 0; i < tonelength; i++)
	{
		if(i%2)
		{
			A_H_ON;
			C_L_ON;	    
			while(TimerDelayus(MUSIC_FREQ_LIST[note - 1]));
			A_H_OFF;
			C_L_OFF;
			while(TimerDelayus(MUSIC_FREQ_LIST[note - 1]));
		}
		else
		{
			A_H_ON;
			B_L_ON;	    
			while(TimerDelayus(MUSIC_FREQ_LIST[note - 1]));
			A_H_OFF;
			B_L_OFF;
			while(TimerDelayus(MUSIC_FREQ_LIST[note - 1]));		
		}
	}
	FET_OFF;
	while(TimerDelayms(pauselength*2));			
}

